setwd("E://OneDrive//1-教学//科技论文写作//课件//课上使用的文档//2.9.5-科研绘图//点线图")

library(xlsx)
library(ggplot2)
library(RColorBrewer)

data <- read.xlsx("示例数据.xlsx", sheetName = "Sheet2", encoding = "UTF-8")
data
data$Conc <- factor(data$Conc, levels = c("0%", "1%", "2%", "5%", "10%"))

ggplot(data)+
  geom_pointrange(aes(x = Conc, y = mean, color = MPs, ymin = mean - sd, ymax = mean + sd), position = position_dodge(.75))+
  geom_text(aes(x = Conc, y = mean + sd + 0.1, label = sig., color = MPs), position = position_dodge(.75), show.legend = F)+
  geom_hline(yintercept = data$mean[data$Conc == "0%"], color = brewer.pal(8,"Dark2")[1], linetype = 2)+
  labs(x = "Concentration", y = "Biomass (g)")+
  scale_color_manual(values = brewer.pal(8,"Dark2"))+
  scale_fill_manual(values = brewer.pal(8,"Dark2"))+
  theme_bw()

Fig <- ggplot(data)+
  geom_pointrange(aes(x = Conc, y = mean, color = MPs, ymin = mean - sd, ymax = mean + sd), position = position_dodge(.75))+
  geom_text(aes(x = Conc, y = mean + sd + 0.1, label = sig., color = MPs), position = position_dodge(.75), show.legend = F)+
  geom_hline(yintercept = data$mean[data$Conc == "0%"], color = brewer.pal(8,"Dark2")[1], linetype = 2)+
  labs(x = "Concentration", y = "Biomass (g)")+
  scale_color_manual(values = brewer.pal(8,"Dark2"))+
  scale_fill_manual(values = brewer.pal(8,"Dark2"))+
  theme_bw()

ggsave("Fig.svg", plot = Fig, width = 5, height = 4, bg = "white")


F1 <- ggplot(data)+
  geom_linerange(aes(x = Conc, y = mean, color = MPs, ymin = mean - sd, ymax = mean + sd), position = position_dodge(.75))+
  geom_text(aes(x = Conc, y = mean + sd + 0.1, label = sig., color = MPs), position = position_dodge(.75), show.legend = F)+
  geom_hline(yintercept = data$mean[data$Conc == "0%"], color = brewer.pal(8,"Dark2")[1], linetype = 2)+
  labs(x = "Concentration", y = "Biomass (g)")+
  scale_color_manual(values = brewer.pal(8,"Dark2"))+
  scale_fill_manual(values = brewer.pal(8,"Dark2"))+
  annotate("text", x = "1%", y = 3.2, label = "geom_linerange", 
           parse = T,  color = "black", size=5)+
  theme_bw()

F2 <- ggplot(data)+
  geom_errorbar(aes(x = Conc, color = MPs, ymin = mean - sd, ymax = mean + sd), position = position_dodge(.75))+
  geom_text(aes(x = Conc, y = mean + sd + 0.1, label = sig., color = MPs), position = position_dodge(.75), show.legend = F)+
  geom_hline(yintercept = data$mean[data$Conc == "0%"], color = brewer.pal(8,"Dark2")[1], linetype = 2)+
  labs(x = "Concentration", y = "Biomass (g)")+
  scale_color_manual(values = brewer.pal(8,"Dark2"))+
  scale_fill_manual(values = brewer.pal(8,"Dark2"))+
  annotate("text", x = "1%", y = 3.2, label = "geom_errorbar", 
           parse = T,  color = "black", size=5)+
  theme_bw()


F3 <- ggplot(data)+
  geom_point(aes(x = Conc, y = mean, color = MPs), position = position_dodge(.75))+
  geom_errorbar(aes(x = Conc, color = MPs, ymin = mean - sd, ymax = mean + sd), position = position_dodge(.75))+
  geom_text(aes(x = Conc, y = mean + sd + 0.1, label = sig., color = MPs), position = position_dodge(.75), show.legend = F)+
  geom_hline(yintercept = data$mean[data$Conc == "0%"], color = brewer.pal(8,"Dark2")[1], linetype = 2)+
  labs(x = "Concentration", y = "Biomass (g)")+
  scale_color_manual(values = brewer.pal(8,"Dark2"))+
  scale_fill_manual(values = brewer.pal(8,"Dark2"))+
  annotate("text", x = "2%", y = 3.2, label = "geom_point+geom_errorbar", 
           parse = T,  color = "black", size=5)+
  theme_bw()


F4 <- ggplot(data)+
  geom_errorbarh(aes(y = Conc, color = MPs, xmin = mean - sd, xmax = mean + sd), position = position_dodge(.75))+
  geom_text(aes(y = Conc, x = mean + sd + 0.1, label = sig., color = MPs), position = position_dodge(.75), show.legend = F)+
  geom_vline(xintercept = data$mean[data$Conc == "0%"], color = brewer.pal(8,"Dark2")[1], linetype = 2)+
  labs(y = "Concentration", x = "Biomass (g)")+
  scale_color_manual(values = brewer.pal(8,"Dark2"))+
  scale_fill_manual(values = brewer.pal(8,"Dark2"))+
  annotate("text", y = "1%", x = 3.5, label = "geom_errorbarh", 
           parse = T,  color = "black", size=5)+
  theme_bw()

F5 <- ggplot(data)+
  geom_crossbar(aes(x = Conc, y = mean, color = MPs, ymin = mean - sd, ymax = mean + sd), position = position_dodge2(preserve = "single"), width = 0.75)+
  geom_text(aes(x = Conc, y = mean + sd + 0.1, label = sig., color = MPs), position = position_dodge(.75), show.legend = F)+
  geom_hline(yintercept = data$mean[data$Conc == "0%"], color = brewer.pal(8,"Dark2")[1], linetype = 2)+
  labs(x = "Concentration", y = "Biomass (g)")+
  scale_color_manual(values = brewer.pal(8,"Dark2"))+
  scale_fill_manual(values = brewer.pal(8,"Dark2"))+
  annotate("text", x = "1%", y = 3.2, label = "geom_crossbar", 
           parse = T,  color = "black", size=5)+
  theme_bw()


Fig0 <- ggpubr::ggarrange(Fig, F1, F2, F3, F4, F5,
                        nrow = 2, ncol = 3, common.legend = T, align = "hv",
                        labels = LETTERS, 
                        font.label = list(color = 'black'))


ggsave("Fig0.svg", plot = Fig0, width = 12, height = 6, bg = "white")
